//
//  CarSafeMapViewController.m
//  carcareIOS
//
//  Created by ileo on 15/7/6.
//  Copyright (c) 2015年 baozun. All rights reserved.
//

#import "CarFenceDetailViewController.h"
#import "MapCarFence.h"
#import "RefreshMgr.h"
#import "TipsView.h"
#import "PickerView.h"

@interface CarFenceDetailViewController ()

@property (nonatomic, strong) MapCarFence *mapCarSafe;
@property (nonatomic, strong) CarFenceModel *model;

@property (nonatomic, strong) UIButton *button;
@property (nonatomic, strong) UIButton *radiusB;
@property (nonatomic, strong) UITextField *nameTF;
@property (nonatomic, strong) UILabel *addressL;

@property (nonatomic, strong) PickerView *pickV;

@end

@implementation CarFenceDetailViewController

-(instancetype)initWithFence:(CarFenceModel *)model{
    self = [super init];
    if (self) {
        self.model = model;
    }
    return self;
}

-(instancetype)init{
    self = [super init];
    if (self) {
        [UmengMgr event:ICZ_GEOFence_AddFence];
        self.model = [[CarFenceModel alloc] init];
        self.model.coordinate = [RefreshMgr sharedInstance].refreshCarLocation.location;
        self.model.radius = 100;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"安全区域";
    
    __weak __typeof(self) wself = self;
    
    self.mapCarSafe = [[MapCarFence alloc] init];
    self.mapCarSafe.mapView.frame = self.view.bounds;
    self.mapCarSafe.mapView.height += __MAIN_HEIGHT_64 + 100;
    self.mapCarSafe.mapView.centerY = __MAIN_HEIGHT_64 / 2;
    self.mapCarSafe.CoordinateChange = ^(){
        wself.button.hidden = NO;
    };
    
    [self.view addSubview:self.mapCarSafe.mapView];
    [self.mapCarSafe showCircleWithCoordinate:self.model.coordinate radius:self.model.radius];

    UIView *view = [self createMapCornerView:CGRectMake(10, 10, __MAIN_WIDTH / 3 * 2 - 25 * __MAIN_RATIO_375, __MAIN_WIDTH / 3 * 2 - 25 * __MAIN_RATIO_375)];
    view.center = self.mapCarSafe.mapView.boundsCenter;
    view.centerY -= (37.5 * __MAIN_RATIO_375);
    view.userInteractionEnabled = NO;
    [self.mapCarSafe.mapView addSubview:view];

    
    [self createTopView];
    
    self.button = [ViewFactory normalButtonWithFrame:FactoryRectButtonDefault(__MAIN_HEIGHT_64 - 50) title:@"保存安全区域" inView:self.view action:^{
        if (![Check isVisiter]) {
            wself.model.coordinate = wself.mapCarSafe.coordinate;
            wself.model.radius = wself.mapCarSafe.radius;
            wself.model.name = wself.nameTF.text;
            wself.model.address = wself.addressL.text;
            if ([wself.nameTF.text isNotNil]) {
                [wself saveFinish:nil];
            }else{
                [AlertMgr showShortTextTips:@[@"请输入安全区域的名称"]];
            }
        }
    }];
    self.button.hidden = YES;
    
    NSArray *radiuss = @[@"100米",@"200米",@"300米",@"400米",@"500米",@"600米",@"700米",@"800米",@"900米",@"1000米",@"2000米",@"3000米",@"4000米",@"5000米"];
    self.pickV = [[PickerView alloc] initWithDatas:radiuss clickOnOK:^(NSInteger index) {
        [AlertMgr dismiss:TYPE_VIEW_POPUP];
        NSString *str = radiuss[index];
        NSInteger num = [[str substringToIndex:str.length - 1] integerValue];
        if (num != wself.model.radius) {
            wself.button.hidden = NO;
        }
        [wself.radiusB setTitle:[NSString stringWithFormat:@"半径:%@",str] forState:UIControlStateNormal];
        [wself.mapCarSafe showCircleWithCoordinate:wself.mapCarSafe.coordinate radius:num];
    } clickOnCancel:^{
        [AlertMgr dismiss:TYPE_VIEW_POPUP];
    }];
    self.radiusB = [Factory buttonWithFrame:CGRectMake(0, 0, 145, 45) bgColor:[UIColor whiteColor] title:[NSString stringWithFormat:@"半径:%zd米",self.model.radius] textColor:[Theme wordColor] click:^{
        [AlertMgr showPopupView:wself.pickV bottom:0];
    } onView:self.view];
    self.radiusB.center = CGPointMake(__MAIN_WIDTH / 2, __MAIN_HEIGHT_64 / 4 * 3);
    self.radiusB.layer.cornerRadius = 4;
    self.radiusB.layer.borderColor = [Theme lineColor].CGColor;
    self.radiusB.layer.borderWidth = 0.5;
    [self navViewFront];
    
    [Factory buttonWithCenter:CGPointMake(40, self.radiusB.centerY) withImage:ImageNamed(@"loc_show") click:^{
        [wself.mapCarSafe showCircleWithCoordinate:[RefreshMgr sharedInstance].refreshCarLocation.location radius:wself.mapCarSafe.radius];
    } onView:self.view];
    
    // Do any additional setup after loading the view.
}
- (UIView *)createMapCornerView:(CGRect)frame
{
    UIView * cornerView = [Factory viewWithFrame:frame bgColor:[[Theme mainColor] colorWithAlphaComponent:0.3] onView:nil];
    cornerView.layer.cornerRadius = frame.size.width / 2;
    cornerView.layer.masksToBounds = YES;
    
    CGFloat smallCornerViewW = 20 * __MAIN_RATIO_375;
    UIView * smallCornerView = [Factory viewWithFrame:CGRectMake(0, 0, smallCornerViewW, smallCornerViewW) bgColor:COLOR_HEXSTRING(@"#61C659") onView:cornerView];
    smallCornerView.center = cornerView.boundsCenter;
    smallCornerView.layer.cornerRadius = smallCornerViewW / 2;
    smallCornerView.layer.masksToBounds = YES;
    smallCornerView.layer.borderColor = [UIColor whiteColor].CGColor;
    smallCornerView.layer.borderWidth = 2;
    return cornerView;
}
-(void)createTopView{
    CGFloat height = 84;
    UIView *topV = [Factory viewWithFrame:CGRectMake(0, 10, 340 * __MAIN_RATIO_375, 84) bgColor:[UIColor whiteColor] onView:self.view];
    topV.centerX = __MAIN_WIDTH / 2;
    topV.layer.cornerRadius = 4;
    topV.layer.borderColor = [Theme lineColor].CGColor;
    topV.layer.borderWidth = 0.5;
    [Factory imageViewWithCenter:CGPointMake(20, height / 4) image:ImageNamed(@"fence_name") onView:topV];
    [Factory imageViewWithCenter:CGPointMake(20, height / 4 * 3) image:ImageNamed(@"fence_address") onView:topV];
    [Factory viewWithFrame:CGRectMake(10, height / 2, topV.width - 20, 0.5) bgColor:[Theme lineColor] onView:topV];
    
    self.nameTF = [Factory textFieldWithFrame:CGRectMake(40, 0, topV.width - 50, height / 2) hideText:NO defaultText:@"请输入安全区域名称" onView:topV];
    self.nameTF.text = self.model.name;
    [self registerTextField:self.nameTF];
    self.nameTF.textAlignment = NSTextAlignmentLeft;
    self.addressL = [Factory labelWithFrame:CGRectMake(40, height / 2, topV.width - 50, height / 2) font:FONT_DEFAULT_Light(15) text:nil textColor:[Theme wordColor] onView:topV textAlignment:NSTextAlignmentLeft];
    __weak __typeof(self) wself = self;
    self.mapCarSafe.Address = ^(NSString *addr){
        wself.addressL.text = addr;
    };
}

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self.mapCarSafe showCircleWithCoordinate:self.model.coordinate radius:self.model.radius];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)saveFinish:(void(^)())finish{
    __weak __typeof(self) wself = self;
    if (wself.model.cid != 0) {
        [CarFenceMgr modityFence:wself.model finish:^{
            [AlertMgr showShortTextTips:@[@"保存成功"]];
            wself.button.hidden = YES;
            if (finish) {
                finish();
            }
        }];
    }else{
        [CarFenceMgr addFence:wself.model finish:^(id JSON) {
            wself.model.cid = [JSON[@"id"] integerValue];
            [AlertMgr showShortTextTips:@[@"保存成功"]];
            wself.button.hidden = YES;
            if (finish) {
                finish();
            }
        }];
    }
}

-(void)clickOnNavBack{
    if (self.editing && [self.nameTF.text isNotNil]) {
        __weak __typeof(self) wself = self;
        TipsView *tipV = [TipsView textTipsWithTitle:@"提示" lightTitles:@[@"是否保存当前安全区域?"]];
        [tipV addLeftButton:@"不保存" clickLeft:^{
            [super clickOnNavBack];
            [AlertMgr dismiss:TYPE_VIEW_ALERT];
        } rightButton:@"保存" clickRight:^{
            [wself saveFinish:^{
                [super clickOnNavBack];
            }];
            [AlertMgr dismiss:TYPE_VIEW_ALERT];
        }];
        [AlertMgr showView:tipV];
    }else{
        [super clickOnNavBack];
    }
}


#pragma mark -

-(NSInteger)textFieldMaxNumberWithTextField:(UITextField *)textField{
    return 19;
}

-(void)textFieldDidEndEditing:(UITextField *)textField{
    if (![textField.text isEqualToString:self.model.name]) {
        self.button.hidden = NO;
    }
}

@end
